package cc.vvshop.demo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * Created by tomas on 16-6-16.
 */
@Service
public class AccountServiceImpl implements AccountService {

    //注入账户操作DAO
    @Autowired
    private AccountDao accountDao;

    //注入编程式事务管理模板
    @Autowired
    private TransactionTemplate transactionTemplate;
    /**
     * 转账操作  withOut Transcation
     * @param out 转出的账户
     * @param in 转入的账户
     * @param money 转账的金额
     * @throws
     */
    public void transferNoTranscation(String out, String in,Integer money) {
        accountDao.outMoney(out,money);
        int a=1/0;
        accountDao.inMoney(in,money);
    }
    /**
     * 转账操作 with Transcation
     * @param out 转出的账户
     * @param in 转入的账户
     * @param money 转账的金额
     * @throws
     */
    public void transfer(final String out,final String in,final Integer money) {
        //使用TransactionTemplate对Dao层的操作进行管理来使用它来进行事务管理
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus paramTransactionStatus) {
                accountDao.outMoney(out, money);
                //这里来发生异常查看事务的完整新是否能够完成两步操作保持事务的完整性
                int i =1/0;
                accountDao.inMoney(in, money);
            }
        });
    }
}
